//
//  CXStrategyCollectionViewCell.m
//  GiftThat
//
//  Created by ChuXiang on 16/1/8.
//  Copyright © 2016年 ChuXiang. All rights reserved.
//

#import "CXStrategyCollectionViewCell.h"
#import "CXClassProjectTableViewCell.h"
#import "CXOtherTableViewCell.h"
@interface CXStrategyCollectionViewCell () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *classTable;
@property (nonatomic, strong) NSMutableArray *otherDataSource;

@end

@implementation CXStrategyCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self getData];
        self.classTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.classTable.delegate = self;
        self.classTable.dataSource = self;
        [self.classTable registerClass:[CXOtherTableViewCell class] forCellReuseIdentifier:@"CXOtherTableViewCellIdentifier"];
        [self.classTable registerClass:[CXClassProjectTableViewCell class] forCellReuseIdentifier:@"CXClassProjectTableViewCellIdentifier"];
        [self addSubview:self.classTable];
    }
    return self;
}

- (void)getData {
    [CXNetWorkTool getWithUrl:kClassFicationOtherURL parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            self.otherDataSource = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = result[@"data"];
            NSArray *array = dic[@"channel_groups"];
            for (NSDictionary *dic in array) {
                CXClassProModel *model = [[CXClassProModel alloc] initWithDictionary:dic];
                [self.otherDataSource addObject:model];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.classTable reloadData];
            });
            
        }
    } fail:^(NSError *error) {
        NSLog(@"%@", error);
    }];
    
}


#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
         CXClassProjectTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CXClassProjectTableViewCellIdentifier"];
        //block推到另外一页
        cell.block = ^(void){
            self.block();
        };
        
        cell.nextBlock = ^(NSString *str, NSString *title){
            self.pushProjectBlock (str, title);
        };
        return cell;
    }else {
        CXOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CXOtherTableViewCellIdentifier"];
        cell.model = self.otherDataSource[indexPath.section - 1];
        cell.myBlock = ^(NSString *str, NSString *title){
            self.downPushBlock(str, title);
        };
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 130;
//    }else if (indexPath.section == 3) {
//        return 230;
    }else {
        CXClassProModel *model = self.otherDataSource[indexPath.section - 1];
        NSArray *array = model.channel;
        NSInteger i = array.count % 4;
        if (i == 0) {
            return (array.count / 4) * ((self.width / 4) - 10) + 70;
        }else{
            return (array.count / 4 + 1) * ((self.width / 4) - 10) + 50;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.classTable.frame = CGRectMake(0, 0, self.width, self.height - 113);
}


@end
